package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.SceneCommend;
import cn.wolfcode.trip.base.query.SceneCommendQueryObject;
import cn.wolfcode.trip.base.service.ISceneCommendService;
import cn.wolfcode.trip.base.service.ISceneService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
// 推荐景点的控制器
@Controller
@RequestMapping("/sceneCommend")
public class SceneCommendController {

    @Autowired
    private ISceneCommendService sceneCommendService;
    @Autowired
    private ISceneService sceneService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SceneCommendQueryObject qo) {
        model.addAttribute("pageInfo", sceneCommendService.query(qo));
        model.addAttribute("scenes", sceneService.list());
        return "sceneCommend/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SceneCommend entity, MultipartFile img) {
        //先处理上传图片的问题
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            //把图片上传到本地图库
            String url = UploadUtil.upload(img);
            //替换旧的图片地址
            entity.setCoverUrl(url);
        }
        sceneCommendService.saveOrUpdate(entity);
        return "redirect:/sceneCommend/list.do";
    }

}
